package com.qf.controller;

import com.qf.pojo.ResultData;
import com.qf.pojo.ShoppingCart;
import com.qf.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 87380
 * @date 2021/12/8
 */
@RestController
@CrossOrigin
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/{id}")
    public ResultData<List<ShoppingCart>> showCart(@PathVariable Integer id){
        List<ShoppingCart> list = shoppingCartService.shoppingCartListByUserId(id);
        if (list == null || list.isEmpty()){
            return new ResultData<>(10000,"您的购物车空空如也");
        }
        return new ResultData<>(0,"查询成功",list);
    }

    @PostMapping("/del")
    public ResultData deletePro(@RequestBody List<Integer> id){
        int i = shoppingCartService.deleteByCartId(id);
        if(i == 0){
            return new ResultData(10000,"该商品不存在");
        }
        return new ResultData(0,"删除成功！");
    }

    @PostMapping("/add")
    public ResultData<ShoppingCart> putProToCart(@RequestBody ShoppingCart shoppingCart){
        int i = shoppingCartService.putProduct(shoppingCart);
        if(i == 0){
            return new ResultData(10000,"商品添加错误！");
        }
        return new ResultData(0,"商品已添加至您的购物车！");
    }
}
